<?php
/////////////////////////////////////////////////////////////////////////////
// 这个文件是 ×××× 项目的 ××××
// Copyright (c) 2015 – 2016 http://www.lijian.website
// 时间:2016年1月19日上午12:32:32
// 用户名:Lenovo
// 作   者:小天天天天
/////////////////////////////////////////////////////////////////////////////
namespace Home\Controller;
use Think\Controller;
class IndexController extends BaseController{
	
	// 载入首页
	public function index() {
		$p=isset($_GET['p'])?$_GET['p']+0:1;
		$pnum=24;
		//仅仅在第一页上显示置顶/未过期/的信息
		if ($p==1) {
			$top_infos=$this->get_topinfos();
			$this->assign("top_infos",$top_infos);
		}
		//获取普通/未过期/已审核/的消息
		$this->get_infos($p,$pnum);
		//获取网站标题等等消息
		$this->get_header();
		//获取地区信息
		$this->get_area();
		//***********************************
		//获取分类
		$this->get_topcategory();
		//获取地区分类
		
		
		//*************广**告******************
		//随机显示中上图
		$bres=$this->randImg('中上',2);
		$this->assign('bres',$bres);
		//随机显示中下图
		$res=$this->randImg('中下',10);
		$this->assign('res',$res);
		//随机显示左对联
		$zuoduilian=$this->randImg('左',1);
		$this->assign('zuoduilian',$zuoduilian);
		//随机显示左对联
		$youduilian=$this->randImg('右',1);
		$this->assign('youduilian',$youduilian);
		//***********************************
		//载入模版
		$this->display ();
	}
	//获取网站消息
	private function get_header(){
		$webinfomodel=M("Webinfo");
		$webinfo=$webinfomodel->find(1);
		$gundong=explode(",", $webinfo['web_notice']);
		$this->assign("gundong",$gundong);
		$this->assign("webinfo",$webinfo);
	}
	
	private function get_topinfos(){
		$map=array(
				"is_top"=>1,
				'info_overtime'=>array("gt",time())
		);
		$infomodel=M("Info");
		$top_infos=$infomodel->join('tp_user ON tp_info.user_id = tp_user.user_id')
		->join("tp_category ON tp_info.cat_id= tp_category.cat_id")
		->join("tp_area ON tp_info.area_id= tp_area.id")
		->field("info_id,area_id,user_name,cat_name,area_name,info_title,info_content,info_createtime,info_overtime,
				is_varify,info_linkman,info_tel,info_wechat,info_qq,info_num,is_top,info_adds,
				info_price,info_img,info_miniimg,tp_info.sort_order,top_overtime")
		->where($map)
		->order("info_createtime desc")
		->select();
		foreach ($top_infos as  $key=>$info){
			//将标题取前12个字防止长标题破坏样式
			$top_infos[$key]["info_title"]=mb_substr($info['info_title'], 0,9,'utf-8');
			$top_infos[$key]["info_content"]=mb_substr($info['info_content'], 0,90,'utf-8');
			$top_infos[$key]["info_createtime"]=date("Y-m-d",$info['info_createtime']);
			$top_infos[$key]["info_price"]=$info['info_price']?$info['info_price']:"面议";
			//$infos[$key]["is_varify"]=$info['is_varify']?"yes":"no";
		}
		return $top_infos;
		
	}
	
	//获取所有置顶未过期的消息
	private function get_infos($p,$pnum){
		$map=array(
				"is_top"=>0,
				'info_overtime'=>array("gt",time()),
				"is_varify"=>1
		);
		$infomodel=M("Info");
		$infos=$infomodel->join('tp_user ON tp_info.user_id = tp_user.user_id')
		->join("tp_category ON tp_info.cat_id= tp_category.cat_id")
		->join("tp_area ON tp_info.area_id= tp_area.id")
		->field("info_id,area_id,user_name,cat_name,area_name,info_title,info_content,info_createtime,info_overtime,
				is_varify,info_linkman,info_tel,info_wechat,info_qq,info_num,is_top,info_adds,
				info_price,info_img,info_miniimg,tp_info.sort_order,top_overtime")
		->where($map)
		->page($p,$pnum)
		->order("info_createtime desc")
		->select();
		foreach ($infos as  $key=>$info){
			//将标题取前12个字防止长标题破坏样式
			$infos[$key]["info_title"]=mb_substr($info['info_title'], 0,9,'utf-8');
			$infos[$key]["info_content"]=mb_substr($info['info_content'], 0,90,'utf-8');
			$infos[$key]["info_createtime"]=date("Y-m-d",$info['info_createtime']);
			$infos[$key]["info_price"]=$info['info_price']?$info['info_price']:"面议";
			//$infos[$key]["is_varify"]=$info['is_varify']?"yes":"no";
		}
		$this->assign("infos",$infos);
		
	}
	
	//////////imgad模板需要的变量//////////
	private function randImg($ad_position,$sum) {

		$advert=D('Advert');
		//看广告有多少个,并取出id们 
		$map['ad_position']=array('eq',$ad_position);
		$count=$advert->where($map)->count(); 
		$res=$advert
		->where($map)
		->field('id')
		->select();
		shuffle($res);	
		//打乱这些结果,并取出前$sum个数值ids
		$res=array_slice($res, 0,$sum);
		foreach ($res as $k => $v) {
			$ids[]=$v['id'];
		}

		$ids=implode(",", $ids);
		//取出图片路径
		$map['id']=array('IN',$ids);
		$res=$advert
		->where($map)
		->field('ad_img')
		->order("field(id,$ids)")
		->select();
		//如果某位置值要求显示一个,那就直接返回
		if($sum==1){
			return $res[0];
		}
		//如果要多个,看广告位是否有空余(仅仅针对中下)
		if(count($res)<$sum){
			$zhaoshang=array_fill ( 
				count($res) 
				,  $sum-count($res) 
				,  array(
					"ad_img" =>  "/Public/Home/images/zhaoshang.png" 
					)
			);
			$res=array_merge($res,$zhaoshang);
		}
		//分成两半来循环输出
		return $res=array_chunk($res, $sum/2);

	}
	//////////imgad模板需要的变量//////////
	
	
	//**************************
	//获取分类信息
	private function get_topcategory() {
		$caregorymodel=M("Category");
		$top_categpry=$caregorymodel->where("parent_id=0")->select();
		$this->assign("top_categpry",$top_categpry);
	}
	//获取地区分类
	private function get_area(){
		$areamodel=M("Area");
		$areas=$areamodel->where(1)->select();
		$this->assign("areas",$areas);
	}
	
	
}
	



